home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / tcl / dist6.3 / tclUtil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  36.3 KB  |  1,446 lines

  1. /* 
  2.  * tclUtil.c --
  3.  *
  4.  *    This file contains utility procedures that are used by many Tcl
  5.  *    commands.
  6.  *
  7.  * Copyright 1987-1991 Regents of the University of California
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /user6/ouster/tcl/RCS/tclUtil.c,v 1.62 91/12/02 11:56:29 ouster Exp $ SPRITE (Berkeley)";
  19. #endif
  20.  
  21. #include "tclInt.h"
  22.  
  23. /*
  24.  * The following values are used in the flags returned by Tcl_ScanElement
  25.  * and used by Tcl_ConvertElement.  The value TCL_DONT_USE_BRACES is also
  26.  * defined in tcl.h;  make sure its value doesn't overlap with any of the
  27.  * values below.
  28.  *
  29.  * TCL_DONT_USE_BRACES -    1 means the string mustn't be enclosed in
  30.  *                braces (e.g. it contains unmatched braces,
  31.  *                or ends in a backslash character, or user
  32.  *                just doesn't want braces);  handle all
  33.  *                special characters by adding backslashes.
  34.  * USE_BRACES -            1 means the string contains a special
  35.  *                character that can be handled simply by
  36.  *                enclosing the entire argument in braces.
  37.  * BRACES_UNMATCHED -        1 means that braces aren't properly matched
  38.  *                in the argument.
  39.  */
  40.  
  41. #define USE_BRACES        2
  42. #define BRACES_UNMATCHED    4
  43.  
  44. /*
  45.  * The variable below is set to NULL before invoking regexp functions
  46.  * and checked after those functions.  If an error occurred then regerror
  47.  * will set the variable to point to a (static) error message.  This
  48.  * mechanism unfortunately does not support multi-threading, but then
  49.  * neither does the rest of the regexp facilities.
  50.  */
  51.  
  52. char *tclRegexpError = NULL;
  53.  
  54. /*
  55.  * Function prototypes for local procedures in this file:
  56.  */
  57.  
  58. static void        SetupAppendBuffer _ANSI_ARGS_((Interp *iPtr,
  59.                 int newSpace));
  60.  
  61. /*
  62.  *----------------------------------------------------------------------
  63.  *
  64.  * TclFindElement --
  65.  *
  66.  *    Given a pointer into a Tcl list, locate the first (or next)
  67.  *    element in the list.
  68.  *
  69.  * Results:
  70.  *    The return value is normally TCL_OK, which means that the
  71.  *    element was successfully located.  If TCL_ERROR is returned
  72.  *    it means that list didn't have proper list structure;
  73.  *    interp->result contains a more detailed error message.
  74.  *
  75.  *    If TCL_OK is returned, then *elementPtr will be set to point
  76.  *    to the first element of list, and *nextPtr will be set to point
  77.  *    to the character just after any white space following the last
  78.  *    character that's part of the element.  If this is the last argument
  79.  *    in the list, then *nextPtr will point to the NULL character at the
  80.  *    end of list.  If sizePtr is non-NULL, *sizePtr is filled in with
  81.  *    the number of characters in the element.  If the element is in
  82.  *    braces, then *elementPtr will point to the character after the
  83.  *    opening brace and *sizePtr will not include either of the braces.
  84.  *    If there isn't an element in the list, *sizePtr will be zero, and
  85.  *    both *elementPtr and *termPtr will refer to the null character at
  86.  *    the end of list.  Note:  this procedure does NOT collapse backslash
  87.  *    sequences.
  88.  *
  89.  * Side effects:
  90.  *    None.
  91.  *
  92.  *----------------------------------------------------------------------
  93.  */
  94.  
  95. int
  96. TclFindElement(interp, list, elementPtr, nextPtr, sizePtr, bracePtr)
  97.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  98.     register char *list;    /* String containing Tcl list with zero
  99.                  * or more elements (possibly in braces). */
  100.     char **elementPtr;        /* Fill in with location of first significant
  101.                  * character in first element of list. */
  102.     char **nextPtr;        /* Fill in with location of character just
  103.                  * after all white space following end of
  104.                  * argument (i.e. next argument or end of
  105.                  * list). */
  106.     int *sizePtr;        /* If non-zero, fill in with size of
  107.                  * element. */
  108.     int *bracePtr;        /* If non-zero fill in with non-zero/zero
  109.                  * to indicate that arg was/wasn't
  110.                  * in braces. */
  111. {
  112.     register char *p;
  113.     int openBraces = 0;
  114.     int inQuotes = 0;
  115.     int size;
  116.  
  117.     /*
  118.      * Skim off leading white space and check for an opening brace or
  119.      * quote.   Note:  use of "isascii" below and elsewhere in this
  120.      * procedure is a temporary hack (7/27/90) because Mx uses characters
  121.      * with the high-order bit set for some things.  This should probably
  122.      * be changed back eventually, or all of Tcl should call isascii.
  123.      */
  124.  
  125.     while (isascii(*list) && isspace(*list)) {
  126.     list++;
  127.     }
  128.     if (*list == '{') {
  129.     openBraces = 1;
  130.     list++;
  131.     } else if (*list == '"') {
  132.     inQuotes = 1;
  133.     list++;
  134.     }
  135.     if (bracePtr != 0) {
  136.     *bracePtr = openBraces;
  137.     }
  138.     p = list;
  139.  
  140.     /*
  141.      * Find the end of the element (either a space or a close brace or
  142.      * the end of the string).
  143.      */
  144.  
  145.     while (1) {
  146.     switch (*p) {
  147.  
  148.         /*
  149.          * Open brace: don't treat specially unless the element is
  150.          * in braces.  In this case, keep a nesting count.
  151.          */
  152.  
  153.         case '{':
  154.         if (openBraces != 0) {
  155.             openBraces++;
  156.         }
  157.         break;
  158.  
  159.         /*
  160.          * Close brace: if element is in braces, keep nesting
  161.          * count and quit when the last close brace is seen.
  162.          */
  163.  
  164.         case '}':
  165.         if (openBraces == 1) {
  166.             char *p2;
  167.  
  168.             size = p - list;
  169.             p++;
  170.             if ((isascii(*p) && isspace(*p)) || (*p == 0)) {
  171.             goto done;
  172.             }
  173.             for (p2 = p; (*p2 != 0) && (!isspace(*p2)) && (p2 < p+20);
  174.                 p2++) {
  175.             /* null body */
  176.             }
  177.             Tcl_ResetResult(interp);
  178.             sprintf(interp->result,
  179.                 "list element in braces followed by \"%.*s\" instead of space",
  180.                 p2-p, p);
  181.             return TCL_ERROR;
  182.         } else if (openBraces != 0) {
  183.             openBraces--;
  184.         }
  185.         break;
  186.  
  187.         /*
  188.          * Backslash:  skip over everything up to the end of the
  189.          * backslash sequence.
  190.          */
  191.  
  192.         case '\\': {
  193.         int size;
  194.  
  195.         (void) Tcl_Backslash(p, &size);
  196.         p += size - 1;
  197.         break;
  198.         }
  199.  
  200.         /*
  201.          * Space: ignore if element is in braces or quotes;  otherwise
  202.          * terminate element.
  203.          */
  204.  
  205.         case ' ':
  206.         case '\f':
  207.         case '\n':
  208.         case '\r':
  209.         case '\t':
  210.         case '\v':
  211.         if ((openBraces == 0) && !inQuotes) {
  212.             size = p - list;
  213.             goto done;
  214.         }
  215.         break;
  216.  
  217.         /*
  218.          * Double-quote:  if element is in quotes then terminate it.
  219.          */
  220.  
  221.         case '"':
  222.         if (inQuotes) {
  223.             char *p2;
  224.  
  225.             size = p-list;
  226.             p++;
  227.             if ((isascii(*p) && isspace(*p)) || (*p == 0)) {
  228.             goto done;
  229.             }
  230.             for (p2 = p; (*p2 != 0) && (!isspace(*p2)) && (p2 < p+20);
  231.                 p2++) {
  232.             /* null body */
  233.             }
  234.             Tcl_ResetResult(interp);
  235.             sprintf(interp->result,
  236.                 "list element in quotes followed by \"%.*s\" %s",
  237.                 p2-p, p, "instead of space");
  238.             return TCL_ERROR;
  239.         }
  240.         break;
  241.  
  242.         /*
  243.          * End of list:  terminate element.
  244.          */
  245.  
  246.         case 0:
  247.         if (openBraces != 0) {
  248.             Tcl_SetResult(interp, "unmatched open brace in list",
  249.                 TCL_STATIC);
  250.             return TCL_ERROR;
  251.         } else if (inQuotes) {
  252.             Tcl_SetResult(interp, "unmatched open quote in list",
  253.                 TCL_STATIC);
  254.             return TCL_ERROR;
  255.         }
  256.         size = p - list;
  257.         goto done;
  258.  
  259.     }
  260.     p++;
  261.     }
  262.  
  263.     done:
  264.     while (isascii(*p) && isspace(*p)) {
  265.     p++;
  266.     }
  267.     *elementPtr = list;
  268.     *nextPtr = p;
  269.     if (sizePtr != 0) {
  270.     *sizePtr = size;
  271.     }
  272.     return TCL_OK;
  273. }
  274.  
  275. /*
  276.  *----------------------------------------------------------------------
  277.  *
  278.  * TclCopyAndCollapse --
  279.  *
  280.  *    Copy a string and eliminate any backslashes that aren't in braces.
  281.  *
  282.  * Results:
  283.  *    There is no return value.  Count chars. get copied from src
  284.  *    to dst.  Along the way, if backslash sequences are found outside
  285.  *    braces, the backslashes are eliminated in the copy.
  286.  *    After scanning count chars. from source, a null character is
  287.  *    placed at the end of dst.
  288.  *
  289.  * Side effects:
  290.  *    None.
  291.  *
  292.  *----------------------------------------------------------------------
  293.  */
  294.  
  295. void
  296. TclCopyAndCollapse(count, src, dst)
  297.     int count;            /* Total number of characters to copy
  298.                  * from src. */
  299.     register char *src;        /* Copy from here... */
  300.     register char *dst;        /* ... to here. */
  301. {
  302.     register char c;
  303.     int numRead;
  304.  
  305.     for (c = *src; count > 0; src++, c = *src, count--) {
  306.     if (c == '\\') {
  307.         *dst = Tcl_Backslash(src, &numRead);
  308.         if (*dst != 0) {
  309.         dst++;
  310.         }
  311.         src += numRead-1;
  312.         count -= numRead-1;
  313.     } else {
  314.         *dst = c;
  315.         dst++;
  316.     }
  317.     }
  318.     *dst = 0;
  319. }
  320.  
  321. /*
  322.  *----------------------------------------------------------------------
  323.  *
  324.  * Tcl_SplitList --
  325.  *
  326.  *    Splits a list up into its constituent fields.
  327.  *
  328.  * Results
  329.  *    The return value is normally TCL_OK, which means that
  330.  *    the list was successfully split up.  If TCL_ERROR is
  331.  *    returned, it means that "list" didn't have proper list
  332.  *    structure;  interp->result will contain a more detailed
  333.  *    error message.
  334.  *
  335.  *    *argvPtr will be filled in with the address of an array
  336.  *    whose elements point to the elements of list, in order.
  337.  *    *argcPtr will get filled in with the number of valid elements
  338.  *    in the array.  A single block of memory is dynamically allocated
  339.  *    to hold both the argv array and a copy of the list (with
  340.  *    backslashes and braces removed in the standard way).
  341.  *    The caller must eventually free this memory by calling free()
  342.  *    on *argvPtr.  Note:  *argvPtr and *argcPtr are only modified
  343.  *    if the procedure returns normally.
  344.  *
  345.  * Side effects:
  346.  *    Memory is allocated.
  347.  *
  348.  *----------------------------------------------------------------------
  349.  */
  350.  
  351. int
  352. Tcl_SplitList(interp, list, argcPtr, argvPtr)
  353.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  354.     char *list;            /* Pointer to string with list structure. */
  355.     int *argcPtr;        /* Pointer to location to fill in with
  356.                  * the number of elements in the list. */
  357.     char ***argvPtr;        /* Pointer to place to store pointer to array
  358.                  * of pointers to list elements. */
  359. {
  360.     char **argv;
  361.     register char *p;
  362.     int size, i, result, elSize, brace;
  363.     char *element;
  364.  
  365.     /*
  366.      * Figure out how much space to allocate.  There must be enough
  367.      * space for both the array of pointers and also for a copy of
  368.      * the list.  To estimate the number of pointers needed, count
  369.      * the number of space characters in the list.
  370.      */
  371.  
  372.     for (size = 1, p = list; *p != 0; p++) {
  373.     if (isspace(*p)) {
  374.         size++;
  375.     }
  376.     }
  377.     size++;            /* Leave space for final NULL pointer. */
  378.     argv = (char **) ckalloc((unsigned)
  379.         ((size * sizeof(char *)) + (p - list) + 1));
  380.     for (i = 0, p = ((char *) argv) + size*sizeof(char *);
  381.         *list != 0; i++) {
  382.     result = TclFindElement(interp, list, &element, &list, &elSize, &brace);
  383.     if (result != TCL_OK) {
  384.         ckfree((char *) argv);
  385.         return result;
  386.     }
  387.     if (*element == 0) {
  388.         break;
  389.     }
  390.     if (i >= size) {
  391.         ckfree((char *) argv);
  392.         Tcl_SetResult(interp, "internal error in Tcl_SplitList",
  393.             TCL_STATIC);
  394.         return TCL_ERROR;
  395.     }
  396.     argv[i] = p;
  397.     if (brace) {
  398.         strncpy(p, element, elSize);
  399.         p += elSize;
  400.         *p = 0;
  401.         p++;
  402.     } else {
  403.         TclCopyAndCollapse(elSize, element, p);
  404.         p += elSize+1;
  405.     }
  406.     }
  407.  
  408.     argv[i] = NULL;
  409.     *argvPtr = argv;
  410.     *argcPtr = i;
  411.     return TCL_OK;
  412. }
  413.  
  414. /*
  415.  *----------------------------------------------------------------------
  416.  *
  417.  * Tcl_ScanElement --
  418.  *
  419.  *    This procedure is a companion procedure to Tcl_ConvertElement.
  420.  *    It scans a string to see what needs to be done to it (e.g.
  421.  *    add backslashes or enclosing braces) to make the string into
  422.  *    a valid Tcl list element.
  423.  *
  424.  * Results:
  425.  *    The return value is an overestimate of the number of characters
  426.  *    that will be needed by Tcl_ConvertElement to produce a valid
  427.  *    list element from string.  The word at *flagPtr is filled in
  428.  *    with a value needed by Tcl_ConvertElement when doing the actual
  429.  *    conversion.
  430.  *
  431.  * Side effects:
  432.  *    None.
  433.  *
  434.  *----------------------------------------------------------------------
  435.  */
  436.  
  437. int
  438. Tcl_ScanElement(string, flagPtr)
  439.     char *string;        /* String to convert to Tcl list element. */
  440.     int *flagPtr;        /* Where to store information to guide
  441.                  * Tcl_ConvertElement. */
  442. {
  443.     int flags, nestingLevel;
  444.     register char *p;
  445.  
  446.     /*
  447.      * This procedure and Tcl_ConvertElement together do two things:
  448.      *
  449.      * 1. They produce a proper list, one that will yield back the
  450.      * argument strings when evaluated or when disassembled with
  451.      * Tcl_SplitList.  This is the most important thing.
  452.      * 
  453.      * 2. They try to produce legible output, which means minimizing the
  454.      * use of backslashes (using braces instead).  However, there are
  455.      * some situations where backslashes must be used (e.g. an element
  456.      * like "{abc": the leading brace will have to be backslashed.  For
  457.      * each element, one of three things must be done:
  458.      *
  459.      * (a) Use the element as-is (it doesn't contain anything special
  460.      * characters).  This is the most desirable option.
  461.      *
  462.      * (b) Enclose the element in braces, but leave the contents alone.
  463.      * This happens if the element contains embedded space, or if it
  464.      * contains characters with special interpretation ($, [, ;, or \),
  465.      * or if it starts with a brace or double-quote, or if there are
  466.      * no characters in the element.
  467.      *
  468.      * (c) Don't enclose the element in braces, but add backslashes to
  469.      * prevent special interpretation of special characters.  This is a
  470.      * last resort used when the argument would normally fall under case
  471.      * (b) but contains unmatched braces.  It also occurs if the last
  472.      * character of the argument is a backslash.
  473.      *
  474.      * The procedure figures out how many bytes will be needed to store
  475.      * the result (actually, it overestimates).  It also collects information
  476.      * about the element in the form of a flags word.
  477.      */
  478.  
  479.     nestingLevel = 0;
  480.     flags = 0;
  481.     p = string;
  482.     if ((*p == '{') || (*p == '"') || (*p == 0)) {
  483.     flags |= USE_BRACES;
  484.     }
  485.     for ( ; *p != 0; p++) {
  486.     switch (*p) {
  487.         case '{':
  488.         nestingLevel++;
  489.         break;
  490.         case '}':
  491.         nestingLevel--;
  492.         if (nestingLevel < 0) {
  493.             flags |= TCL_DONT_USE_BRACES|BRACES_UNMATCHED;
  494.         }
  495.         break;
  496.         case '[':
  497.         case '$':
  498.         case ';':
  499.         case ' ':
  500.         case '\f':
  501.         case '\n':
  502.         case '\r':
  503.         case '\t':
  504.         case '\v':
  505.         flags |= USE_BRACES;
  506.         break;
  507.         case '\\':
  508.         if (p[1] == 0) {
  509.             flags = TCL_DONT_USE_BRACES;
  510.         } else {
  511.             int size;
  512.  
  513.             (void) Tcl_Backslash(p, &size);
  514.             p += size-1;
  515.             flags |= USE_BRACES;
  516.         }
  517.         break;
  518.     }
  519.     }
  520.     if (nestingLevel != 0) {
  521.     flags = TCL_DONT_USE_BRACES | BRACES_UNMATCHED;
  522.     }
  523.     *flagPtr = flags;
  524.  
  525.     /*
  526.      * Allow enough space to backslash every character plus leave
  527.      * two spaces for braces.
  528.      */
  529.  
  530.     return 2*(p-string) + 2;
  531. }
  532.  
  533. /*
  534.  *----------------------------------------------------------------------
  535.  *
  536.  * Tcl_ConvertElement --
  537.  *
  538.  *    This is a companion procedure to Tcl_ScanElement.  Given the
  539.  *    information produced by Tcl_ScanElement, this procedure converts
  540.  *    a string to a list element equal to that string.
  541.  *
  542.  * Results:
  543.  *    Information is copied to *dst in the form of a list element
  544.  *    identical to src (i.e. if Tcl_SplitList is applied to dst it
  545.  *    will produce a string identical to src).  The return value is
  546.  *    a count of the number of characters copied (not including the
  547.  *    terminating NULL character).
  548.  *
  549.  * Side effects:
  550.  *    None.
  551.  *
  552.  *----------------------------------------------------------------------
  553.  */
  554.  
  555. int
  556. Tcl_ConvertElement(src, dst, flags)
  557.     register char *src;        /* Source information for list element. */
  558.     char *dst;            /* Place to put list-ified element. */
  559.     int flags;            /* Flags produced by Tcl_ScanElement. */
  560. {
  561.     register char *p = dst;
  562.  
  563.     /*
  564.      * See the comment block at the beginning of the Tcl_ScanElement
  565.      * code for details of how this works.
  566.      */
  567.  
  568.     if ((flags & USE_BRACES) && !(flags & TCL_DONT_USE_BRACES)) {
  569.     *p = '{';
  570.     p++;
  571.     for ( ; *src != 0; src++, p++) {
  572.         *p = *src;
  573.     }
  574.     *p = '}';
  575.     p++;
  576.     } else if (*src == 0) {
  577.     /*
  578.      * If string is empty but can't use braces, then use special
  579.      * backslash sequence that maps to empty string.
  580.      */
  581.  
  582.     p[0] = '\\';
  583.     p[1] = '0';
  584.     p += 2;
  585.     } else {
  586.     for (; *src != 0 ; src++) {
  587.         switch (*src) {
  588.         case ']':
  589.         case '[':
  590.         case '$':
  591.         case ';':
  592.         case ' ':
  593.         case '\\':
  594.         case '"':
  595.             *p = '\\';
  596.             p++;
  597.             break;
  598.         case '{':
  599.         case '}':
  600.             if (flags & BRACES_UNMATCHED) {
  601.             *p = '\\';
  602.             p++;
  603.             }
  604.             break;
  605.         case '\f':
  606.             *p = '\\';
  607.             p++;
  608.             *p = 'f';
  609.             p++;
  610.             continue;
  611.         case '\n':
  612.             *p = '\\';
  613.             p++;
  614.             *p = 'n';
  615.             p++;
  616.             continue;
  617.         case '\r':
  618.             *p = '\\';
  619.             p++;
  620.             *p = 'r';
  621.             p++;
  622.             continue;
  623.         case '\t':
  624.             *p = '\\';
  625.             p++;
  626.             *p = 't';
  627.             p++;
  628.             continue;
  629.         case '\v':
  630.             *p = '\\';
  631.             p++;
  632.             *p = 'v';
  633.             p++;
  634.             continue;
  635.         }
  636.         *p = *src;
  637.         p++;
  638.     }
  639.     }
  640.     *p = '\0';
  641.     return p-dst;
  642. }
  643.  
  644. /*
  645.  *----------------------------------------------------------------------
  646.  *
  647.  * Tcl_Merge --
  648.  *
  649.  *    Given a collection of strings, merge them together into a
  650.  *    single string that has proper Tcl list structured (i.e.
  651.  *    Tcl_SplitList may be used to retrieve strings equal to the
  652.  *    original elements, and Tcl_Eval will parse the string back
  653.  *    into its original elements).
  654.  *
  655.  * Results:
  656.  *    The return value is the address of a dynamically-allocated
  657.  *    string containing the merged list.
  658.  *
  659.  * Side effects:
  660.  *    None.
  661.  *
  662.  *----------------------------------------------------------------------
  663.  */
  664.  
  665. char *
  666. Tcl_Merge(argc, argv)
  667.     int argc;            /* How many strings to merge. */
  668.     char **argv;        /* Array of string values. */
  669. {
  670. #   define LOCAL_SIZE 20
  671.     int localFlags[LOCAL_SIZE], *flagPtr;
  672.     int numChars;
  673.     char *result;
  674.     register char *dst;
  675.     int i;
  676.  
  677.     /*
  678.      * Pass 1: estimate space, gather flags.
  679.      */
  680.  
  681.     if (argc <= LOCAL_SIZE) {
  682.     flagPtr = localFlags;
  683.     } else {
  684.     flagPtr = (int *) ckalloc((unsigned) argc*sizeof(int));
  685.     }
  686.     numChars = 1;
  687.     for (i = 0; i < argc; i++) {
  688.     numChars += Tcl_ScanElement(argv[i], &flagPtr[i]) + 1;
  689.     }
  690.  
  691.     /*
  692.      * Pass two: copy into the result area.
  693.      */
  694.  
  695.     result = (char *) ckalloc((unsigned) numChars);
  696.     dst = result;
  697.     for (i = 0; i < argc; i++) {
  698.     numChars = Tcl_ConvertElement(argv[i], dst, flagPtr[i]);
  699.     dst += numChars;
  700.     *dst = ' ';
  701.     dst++;
  702.     }
  703.     if (dst == result) {
  704.     *dst = 0;
  705.     } else {
  706.     dst[-1] = 0;
  707.     }
  708.  
  709.     if (flagPtr != localFlags) {
  710.     ckfree((char *) flagPtr);
  711.     }
  712.     return result;
  713. }
  714.  
  715. /*
  716.  *----------------------------------------------------------------------
  717.  *
  718.  * Tcl_Concat --
  719.  *
  720.  *    Concatenate a set of strings into a single large string.
  721.  *
  722.  * Results:
  723.  *    The return value is dynamically-allocated string containing
  724.  *    a concatenation of all the strings in argv, with spaces between
  725.  *    the original argv elements.
  726.  *
  727.  * Side effects:
  728.  *    Memory is allocated for the result;  the caller is responsible
  729.  *    for freeing the memory.
  730.  *
  731.  *----------------------------------------------------------------------
  732.  */
  733.  
  734. char *
  735. Tcl_Concat(argc, argv)
  736.     int argc;            /* Number of strings to concatenate. */
  737.     char **argv;        /* Array of strings to concatenate. */
  738. {
  739.     int totalSize, i;
  740.     register char *p;
  741.     char *result;
  742.  
  743.     for (totalSize = 1, i = 0; i < argc; i++) {
  744.     totalSize += strlen(argv[i]) + 1;
  745.     }
  746.     result = (char *) ckalloc((unsigned) totalSize);
  747.     if (argc == 0) {
  748.     *result = '\0';
  749.     return result;
  750.     }
  751.     for (p = result, i = 0; i < argc; i++) {
  752.     char *element;
  753.     int length;
  754.  
  755.     /*
  756.      * Clip white space off the front and back of the string
  757.      * to generate a neater result, and ignore any empty
  758.      * elements.
  759.      */
  760.  
  761.     element = argv[i];
  762.     while (isspace(*element)) {
  763.         element++;
  764.     }
  765.     for (length = strlen(element);
  766.         (length > 0) && (isspace(element[length-1]));
  767.         length--) {
  768.         /* Null loop body. */
  769.     }
  770.     if (length == 0) {
  771.         continue;
  772.     }
  773.     (void) strncpy(p, element, length);
  774.     p += length;
  775.     *p = ' ';
  776.     p++;
  777.     }
  778.     if (p != result) {
  779.     p[-1] = 0;
  780.     } else {
  781.     *p = 0;
  782.     }
  783.     return result;
  784. }
  785.  
  786. /*
  787.  *----------------------------------------------------------------------
  788.  *
  789.  * Tcl_StringMatch --
  790.  *
  791.  *    See if a particular string matches a particular pattern.
  792.  *
  793.  * Results:
  794.  *    The return value is 1 if string matches pattern, and
  795.  *    0 otherwise.  The matching operation permits the following
  796.  *    special characters in the pattern: *?\[] (see the manual
  797.  *    entry for details on what these mean).
  798.  *
  799.  * Side effects:
  800.  *    None.
  801.  *
  802.  *----------------------------------------------------------------------
  803.  */
  804.  
  805. int
  806. Tcl_StringMatch(string, pattern)
  807.     register char *string;    /* String. */
  808.     register char *pattern;    /* Pattern, which may contain
  809.                  * special characters. */
  810. {
  811.     char c2;
  812.  
  813.     while (1) {
  814.     /* See if we're at the end of both the pattern and the string.
  815.      * If so, we succeeded.  If we're at the end of the pattern
  816.      * but not at the end of the string, we failed.
  817.      */
  818.     
  819.     if (*pattern == 0) {
  820.         if (*string == 0) {
  821.         return 1;
  822.         } else {
  823.         return 0;
  824.         }
  825.     }
  826.     if ((*string == 0) && (*pattern != '*')) {
  827.         return 0;
  828.     }
  829.  
  830.     /* Check for a "*" as the next pattern character.  It matches
  831.      * any substring.  We handle this by calling ourselves
  832.      * recursively for each postfix of string, until either we
  833.      * match or we reach the end of the string.
  834.      */
  835.     
  836.     if (*pattern == '*') {
  837.         pattern += 1;
  838.         if (*pattern == 0) {
  839.         return 1;
  840.         }
  841.         while (*string != 0) {
  842.         if (Tcl_StringMatch(string, pattern)) {
  843.             return 1;
  844.         }
  845.         string += 1;
  846.         }
  847.         return 0;
  848.     }
  849.     
  850.     /* Check for a "?" as the next pattern character.  It matches
  851.      * any single character.
  852.      */
  853.  
  854.     if (*pattern == '?') {
  855.         goto thisCharOK;
  856.     }
  857.  
  858.     /* Check for a "[" as the next pattern character.  It is followed
  859.      * by a list of characters that are acceptable, or by a range
  860.      * (two characters separated by "-").
  861.      */
  862.     
  863.     if (*pattern == '[') {
  864.         pattern += 1;
  865.         while (1) {
  866.         if ((*pattern == ']') || (*pattern == 0)) {
  867.             return 0;
  868.         }
  869.         if (*pattern == *string) {
  870.             break;
  871.         }
  872.         if (pattern[1] == '-') {
  873.             c2 = pattern[2];
  874.             if (c2 == 0) {
  875.             return 0;
  876.             }
  877.             if ((*pattern <= *string) && (c2 >= *string)) {
  878.             break;
  879.             }
  880.             if ((*pattern >= *string) && (c2 <= *string)) {
  881.             break;
  882.             }
  883.             pattern += 2;
  884.         }
  885.         pattern += 1;
  886.         }
  887.         while ((*pattern != ']') && (*pattern != 0)) {
  888.         pattern += 1;
  889.         }
  890.         goto thisCharOK;
  891.     }
  892.     
  893.     /* If the next pattern character is '/', just strip off the '/'
  894.      * so we do exact matching on the character that follows.
  895.      */
  896.     
  897.     if (*pattern == '\\') {
  898.         pattern += 1;
  899.         if (*pattern == 0) {
  900.         return 0;
  901.         }
  902.     }
  903.  
  904.     /* There's no special character.  Just make sure that the next
  905.      * characters of each string match.
  906.      */
  907.     
  908.     if (*pattern != *string) {
  909.         return 0;
  910.     }
  911.  
  912.     thisCharOK: pattern += 1;
  913.     string += 1;
  914.     }
  915. }
  916.  
  917. /*
  918.  *----------------------------------------------------------------------
  919.  *
  920.  * Tcl_SetResult --
  921.  *
  922.  *    Arrange for "string" to be the Tcl return value.
  923.  *
  924.  * Results:
  925.  *    None.
  926.  *
  927.  * Side effects:
  928.  *    interp->result is left pointing either to "string" (if "copy" is 0)
  929.  *    or to a copy of string.
  930.  *
  931.  *----------------------------------------------------------------------
  932.  */
  933.  
  934. void
  935. Tcl_SetResult(interp, string, freeProc)
  936.     Tcl_Interp *interp;        /* Interpreter with which to associate the
  937.                  * return value. */
  938.     char *string;        /* Value to be returned.  If NULL,
  939.                  * the result is set to an empty string. */
  940.     Tcl_FreeProc *freeProc;    /* Gives information about the string:
  941.                  * TCL_STATIC, TCL_VOLATILE, or the address
  942.                  * of a Tcl_FreeProc such as free. */
  943. {
  944.     register Interp *iPtr = (Interp *) interp;
  945.     int length;
  946.     Tcl_FreeProc *oldFreeProc = iPtr->freeProc;
  947.     char *oldResult = iPtr->result;
  948.  
  949.     iPtr->freeProc = freeProc;
  950.     if (string == NULL) {
  951.     iPtr->resultSpace[0] = 0;
  952.     iPtr->result = iPtr->resultSpace;
  953.     iPtr->freeProc = 0;
  954.     } else if (freeProc == TCL_VOLATILE) {
  955.     length = strlen(string);
  956.     if (length > TCL_RESULT_SIZE) {
  957.         iPtr->result = (char *) ckalloc((unsigned) length+1);
  958.         iPtr->freeProc = (Tcl_FreeProc *) free;
  959.     } else {
  960.         iPtr->result = iPtr->resultSpace;
  961.         iPtr->freeProc = 0;
  962.     }
  963.     strcpy(iPtr->result, string);
  964.     } else {
  965.     iPtr->result = string;
  966.     }
  967.  
  968.     /*
  969.      * If the old result was dynamically-allocated, free it up.  Do it
  970.      * here, rather than at the beginning, in case the new result value
  971.      * was part of the old result value.
  972.      */
  973.  
  974.     if (oldFreeProc != 0) {
  975.     (*oldFreeProc)(oldResult);
  976.     }
  977. }
  978.  
  979. /*
  980.  *----------------------------------------------------------------------
  981.  *
  982.  * Tcl_AppendResult --
  983.  *
  984.  *    Append a variable number of strings onto the result already
  985.  *    present for an interpreter.
  986.  *
  987.  * Results:
  988.  *    None.
  989.  *
  990.  * Side effects:
  991.  *    The result in the interpreter given by the first argument
  992.  *    is extended by the strings given by the second and following
  993.  *    arguments (up to a terminating NULL argument).
  994.  *
  995.  *----------------------------------------------------------------------
  996.  */
  997.  
  998.     /* VARARGS2 */
  999. #ifndef lint
  1000. void
  1001. Tcl_AppendResult(va_alist)
  1002. #else
  1003. void
  1004.     /* VARARGS2 */ /* ARGSUSED */
  1005. Tcl_AppendResult(interp, p, va_alist)
  1006.     Tcl_Interp *interp;        /* Interpreter whose result is to be
  1007.                  * extended. */
  1008.     char *p;            /* One or more strings to add to the
  1009.                  * result, terminated with NULL. */
  1010. #endif
  1011.     va_dcl
  1012. {
  1013.     va_list argList;
  1014.     register Interp *iPtr;
  1015.     char *string;
  1016.     int newSpace;
  1017.  
  1018.     /*
  1019.      * First, scan through all the arguments to see how much space is
  1020.      * needed.
  1021.      */
  1022.  
  1023.     va_start(argList);
  1024.     iPtr = va_arg(argList, Interp *);
  1025.     newSpace = 0;
  1026.     while (1) {
  1027.     string = va_arg(argList, char *);
  1028.     if (string == NULL) {
  1029.         break;
  1030.     }
  1031.     newSpace += strlen(string);
  1032.     }
  1033.     va_end(argList);
  1034.  
  1035.     /*
  1036.      * If the append buffer isn't already setup and large enough
  1037.      * to hold the new data, set it up.
  1038.      */
  1039.  
  1040.     if ((iPtr->result != iPtr->appendResult)
  1041.        || ((newSpace + iPtr->appendUsed) >= iPtr->appendAvl)) {
  1042.        SetupAppendBuffer(iPtr, newSpace);
  1043.     }
  1044.  
  1045.     /*
  1046.      * Final step:  go through all the argument strings again, copying
  1047.      * them into the buffer.
  1048.      */
  1049.  
  1050.     va_start(argList);
  1051.     (void) va_arg(argList, Tcl_Interp *);
  1052.     while (1) {
  1053.     string = va_arg(argList, char *);
  1054.     if (string == NULL) {
  1055.         break;
  1056.     }
  1057.     strcpy(iPtr->appendResult + iPtr->appendUsed, string);
  1058.     iPtr->appendUsed += strlen(string);
  1059.     }
  1060.     va_end(argList);
  1061. }
  1062.  
  1063. /*
  1064.  *----------------------------------------------------------------------
  1065.  *
  1066.  * Tcl_AppendElement --
  1067.  *
  1068.  *    Convert a string to a valid Tcl list element and append it
  1069.  *    to the current result (which is ostensibly a list).
  1070.  *
  1071.  * Results:
  1072.  *    None.
  1073.  *
  1074.  * Side effects:
  1075.  *    The result in the interpreter given by the first argument
  1076.  *    is extended with a list element converted from string.  If
  1077.  *    the original result wasn't empty, then a blank is added before
  1078.  *    the converted list element.
  1079.  *
  1080.  *----------------------------------------------------------------------
  1081.  */
  1082.  
  1083. void
  1084. Tcl_AppendElement(interp, string, noSep)
  1085.     Tcl_Interp *interp;        /* Interpreter whose result is to be
  1086.                  * extended. */
  1087.     char *string;        /* String to convert to list element and
  1088.                  * add to result. */
  1089.     int noSep;            /* If non-zero, then don't output a
  1090.                  * space character before this element,
  1091.                  * even if the element isn't the first
  1092.                  * thing in the output buffer. */
  1093. {
  1094.     register Interp *iPtr = (Interp *) interp;
  1095.     int size, flags;
  1096.     char *dst;
  1097.  
  1098.     /*
  1099.      * See how much space is needed, and grow the append buffer if
  1100.      * needed to accommodate the list element.
  1101.      */
  1102.  
  1103.     size = Tcl_ScanElement(string, &flags) + 1;
  1104.     if ((iPtr->result != iPtr->appendResult)
  1105.        || ((size + iPtr->appendUsed) >= iPtr->appendAvl)) {
  1106.        SetupAppendBuffer(iPtr, size+iPtr->appendUsed);
  1107.     }
  1108.  
  1109.     /*
  1110.      * Convert the string into a list element and copy it to the
  1111.      * buffer that's forming.
  1112.      */
  1113.  
  1114.     dst = iPtr->appendResult + iPtr->appendUsed;
  1115.     if (!noSep && (iPtr->appendUsed != 0)) {
  1116.     iPtr->appendUsed++;
  1117.     *dst = ' ';
  1118.     dst++;
  1119.     }
  1120.     iPtr->appendUsed += Tcl_ConvertElement(string, dst, flags);
  1121. }
  1122.  
  1123. /*
  1124.  *----------------------------------------------------------------------
  1125.  *
  1126.  * SetupAppendBuffer --
  1127.  *
  1128.  *    This procedure makes sure that there is an append buffer
  1129.  *    properly initialized for interp, and that it has at least
  1130.  *    enough room to accommodate newSpace new bytes of information.
  1131.  *
  1132.  * Results:
  1133.  *    None.
  1134.  *
  1135.  * Side effects:
  1136.  *    None.
  1137.  *
  1138.  *----------------------------------------------------------------------
  1139.  */
  1140.  
  1141. static void
  1142. SetupAppendBuffer(iPtr, newSpace)
  1143.     register Interp *iPtr;    /* Interpreter whose result is being set up. */
  1144.     int newSpace;        /* Make sure that at least this many bytes
  1145.                  * of new information may be added. */
  1146. {
  1147.     int totalSpace;
  1148.  
  1149.     /*
  1150.      * Make the append buffer larger, if that's necessary, then
  1151.      * copy the current result into the append buffer and make the
  1152.      * append buffer the official Tcl result.
  1153.      */
  1154.  
  1155.     if (iPtr->result != iPtr->appendResult) {
  1156.     /*
  1157.      * If an oversized buffer was used recently, then free it up
  1158.      * so we go back to a smaller buffer.  This avoids tying up
  1159.      * memory forever after a large operation.
  1160.      */
  1161.  
  1162.     if (iPtr->appendAvl > 500) {
  1163.         ckfree(iPtr->appendResult);
  1164.         iPtr->appendResult = NULL;
  1165.         iPtr->appendAvl = 0;
  1166.     }
  1167.     iPtr->appendUsed = strlen(iPtr->result);
  1168.     }
  1169.     totalSpace = newSpace + iPtr->appendUsed;
  1170.     if (totalSpace >= iPtr->appendAvl) {
  1171.     char *new;
  1172.  
  1173.     if (totalSpace < 100) {
  1174.         totalSpace = 200;
  1175.     } else {
  1176.         totalSpace *= 2;
  1177.     }
  1178.     new = (char *) ckalloc((unsigned) totalSpace);
  1179.     strcpy(new, iPtr->result);
  1180.     if (iPtr->appendResult != NULL) {
  1181.         ckfree(iPtr->appendResult);
  1182.     }
  1183.     iPtr->appendResult = new;
  1184.     iPtr->appendAvl = totalSpace;
  1185.     } else if (iPtr->result != iPtr->appendResult) {
  1186.     strcpy(iPtr->appendResult, iPtr->result);
  1187.     }
  1188.     Tcl_FreeResult(iPtr);
  1189.     iPtr->result = iPtr->appendResult;
  1190. }
  1191.  
  1192. /*
  1193.  *----------------------------------------------------------------------
  1194.  *
  1195.  * Tcl_ResetResult --
  1196.  *
  1197.  *    This procedure restores the result area for an interpreter
  1198.  *    to its default initialized state, freeing up any memory that
  1199.  *    may have been allocated for the result and clearing any
  1200.  *    error information for the interpreter.
  1201.  *
  1202.  * Results:
  1203.  *    None.
  1204.  *
  1205.  * Side effects:
  1206.  *    None.
  1207.  *
  1208.  *----------------------------------------------------------------------
  1209.  */
  1210.  
  1211. void
  1212. Tcl_ResetResult(interp)
  1213.     Tcl_Interp *interp;        /* Interpreter for which to clear result. */
  1214. {
  1215.     register Interp *iPtr = (Interp *) interp;
  1216.  
  1217.     Tcl_FreeResult(iPtr);
  1218.     iPtr->result = iPtr->resultSpace;
  1219.     iPtr->resultSpace[0] = 0;
  1220.     iPtr->flags &=
  1221.         ~(ERR_ALREADY_LOGGED | ERR_IN_PROGRESS | ERROR_CODE_SET);
  1222. }
  1223.  
  1224. /*
  1225.  *----------------------------------------------------------------------
  1226.  *
  1227.  * Tcl_SetErrorCode --
  1228.  *
  1229.  *    This procedure is called to record machine-readable information
  1230.  *    about an error that is about to be returned.
  1231.  *
  1232.  * Results:
  1233.  *    None.
  1234.  *
  1235.  * Side effects:
  1236.  *    The errorCode global variable is modified to hold all of the
  1237.  *    arguments to this procedure, in a list form with each argument
  1238.  *    becoming one element of the list.  A flag is set internally
  1239.  *    to remember that errorCode has been set, so the variable doesn't
  1240.  *    get set automatically when the error is returned.
  1241.  *
  1242.  *----------------------------------------------------------------------
  1243.  */
  1244.     /* VARARGS2 */
  1245. #ifndef lint
  1246. void
  1247. Tcl_SetErrorCode(va_alist)
  1248. #else
  1249. void
  1250.     /* VARARGS2 */ /* ARGSUSED */
  1251. Tcl_SetErrorCode(interp, p, va_alist)
  1252.     Tcl_Interp *interp;        /* Interpreter whose errorCode variable is
  1253.                  * to be set. */
  1254.     char *p;            /* One or more elements to add to errorCode,
  1255.                  * terminated with NULL. */
  1256. #endif
  1257.     va_dcl
  1258. {
  1259.     va_list argList;
  1260.     char *string;
  1261.     int flags;
  1262.     Interp *iPtr;
  1263.  
  1264.     /*
  1265.      * Scan through the arguments one at a time, appending them to
  1266.      * $errorCode as list elements.
  1267.      */
  1268.  
  1269.     va_start(argList);
  1270.     iPtr = va_arg(argList, Interp *);
  1271.     flags = TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT;
  1272.     while (1) {
  1273.     string = va_arg(argList, char *);
  1274.     if (string == NULL) {
  1275.         break;
  1276.     }
  1277.     (void) Tcl_SetVar2((Tcl_Interp *) iPtr, "errorCode",
  1278.         (char *) NULL, string, flags);
  1279.     flags |= TCL_APPEND_VALUE;
  1280.     }
  1281.     va_end(argList);
  1282.     iPtr->flags |= ERROR_CODE_SET;
  1283. }
  1284.  
  1285. /*
  1286.  *----------------------------------------------------------------------
  1287.  *
  1288.  * TclGetListIndex --
  1289.  *
  1290.  *    Parse a list index, which may be either an integer or the
  1291.  *    value "end".
  1292.  *
  1293.  * Results:
  1294.  *    The return value is either TCL_OK or TCL_ERROR.  If it is
  1295.  *    TCL_OK, then the index corresponding to string is left in
  1296.  *    *indexPtr.  If the return value is TCL_ERROR, then string
  1297.  *    was bogus;  an error message is returned in interp->result.
  1298.  *    If a negative index is specified, it is rounded up to 0.
  1299.  *    The index value may be larger than the size of the list
  1300.  *    (this happens when "end" is specified).
  1301.  *
  1302.  * Side effects:
  1303.  *    None.
  1304.  *
  1305.  *----------------------------------------------------------------------
  1306.  */
  1307.  
  1308. int
  1309. TclGetListIndex(interp, string, indexPtr)
  1310.     Tcl_Interp *interp;            /* Interpreter for error reporting. */
  1311.     char *string;            /* String containing list index. */
  1312.     int *indexPtr;            /* Where to store index. */
  1313. {
  1314.     if (isdigit(*string) || (*string == '-')) {
  1315.     if (Tcl_GetInt(interp, string, indexPtr) != TCL_OK) {
  1316.         return TCL_ERROR;
  1317.     }
  1318.     if (*indexPtr < 0) {
  1319.         *indexPtr = 0;
  1320.     }
  1321.     } else if (strncmp(string, "end", strlen(string)) == 0) {
  1322.     *indexPtr = 1<<30;
  1323.     } else {
  1324.     Tcl_AppendResult(interp, "bad index \"", string,
  1325.         "\": must be integer or \"end\"", (char *) NULL);
  1326.     return TCL_ERROR;
  1327.     }
  1328.     return TCL_OK;
  1329. }
  1330.  
  1331. /*
  1332.  *----------------------------------------------------------------------
  1333.  *
  1334.  * TclCompileRegexp --
  1335.  *
  1336.  *    Compile a regular expression into a form suitable for fast
  1337.  *    matching.  This procedure retains a small cache of pre-compiled
  1338.  *    regular expressions in the interpreter, in order to avoid
  1339.  *    compilation costs as much as possible.
  1340.  *
  1341.  * Results:
  1342.  *    The return value is a pointer to the compiled form of string,
  1343.  *    suitable for passing to regexec.  If an error occurred while
  1344.  *    compiling the pattern, then NULL is returned and an error
  1345.  *    message is left in interp->result.
  1346.  *
  1347.  * Side effects:
  1348.  *    The cache of compiled regexp's in interp will be modified to
  1349.  *    hold information for string, if such information isn't already
  1350.  *    present in the cache.
  1351.  *
  1352.  *----------------------------------------------------------------------
  1353.  */
  1354.  
  1355. regexp *
  1356. TclCompileRegexp(interp, string)
  1357.     Tcl_Interp *interp;            /* For use in error reporting. */
  1358.     char *string;            /* String for which to produce
  1359.                      * compiled regular expression. */
  1360. {
  1361.     register Interp *iPtr = (Interp *) interp;
  1362.     int i, length;
  1363.     regexp *result;
  1364.  
  1365.     length = strlen(string);
  1366.     for (i = 0; i < NUM_REGEXPS; i++) {
  1367.     if ((length == iPtr->patLengths[i])
  1368.         && (strcmp(string, iPtr->patterns[i]) == 0)) {
  1369.         /*
  1370.          * Move the matched pattern to the first slot in the
  1371.          * cache and shift the other patterns down one position.
  1372.          */
  1373.  
  1374.         if (i != 0) {
  1375.         int j;
  1376.         char *cachedString;
  1377.  
  1378.         cachedString = iPtr->patterns[i];
  1379.         result = iPtr->regexps[i];
  1380.         for (j = i-1; j >= 0; j--) {
  1381.             iPtr->patterns[j+1] = iPtr->patterns[j];
  1382.             iPtr->patLengths[j+1] = iPtr->patLengths[j];
  1383.             iPtr->regexps[j+1] = iPtr->regexps[j];
  1384.         }
  1385.         iPtr->patterns[0] = cachedString;
  1386.         iPtr->patLengths[0] = length;
  1387.         iPtr->regexps[0] = result;
  1388.         }
  1389.         return iPtr->regexps[0];
  1390.     }
  1391.     }
  1392.  
  1393.     /*
  1394.      * No match in the cache.  Compile the string and add it to the
  1395.      * cache.
  1396.      */
  1397.  
  1398.     tclRegexpError = NULL;
  1399.     result = regcomp(string);
  1400.     if (tclRegexpError != NULL) {
  1401.     Tcl_AppendResult(interp,
  1402.         "couldn't compile regular expression pattern: ",
  1403.         tclRegexpError, (char *) NULL);
  1404.     return NULL;
  1405.     }
  1406.     if (iPtr->patterns[NUM_REGEXPS-1] != NULL) {
  1407.     ckfree(iPtr->patterns[NUM_REGEXPS-1]);
  1408.     ckfree((char *) iPtr->regexps[NUM_REGEXPS-1]);
  1409.     }
  1410.     for (i = NUM_REGEXPS - 2; i >= 0; i--) {
  1411.     iPtr->patterns[i+1] = iPtr->patterns[i];
  1412.     iPtr->patLengths[i+1] = iPtr->patLengths[i];
  1413.     iPtr->regexps[i+1] = iPtr->regexps[i];
  1414.     }
  1415.     iPtr->patterns[0] = (char *) ckalloc((unsigned) (length+1));
  1416.     strcpy(iPtr->patterns[0], string);
  1417.     iPtr->patLengths[0] = length;
  1418.     iPtr->regexps[0] = result;
  1419.     return result;
  1420. }
  1421.  
  1422. /*
  1423.  *----------------------------------------------------------------------
  1424.  *
  1425.  * regerror --
  1426.  *
  1427.  *    This procedure is invoked by the Henry Spencer's regexp code
  1428.  *    when an error occurs.  It saves the error message so it can
  1429.  *    be seen by the code that called Spencer's code.
  1430.  *
  1431.  * Results:
  1432.  *    None.
  1433.  *
  1434.  * Side effects:
  1435.  *    The value of "string" is saved in "tclRegexpError".
  1436.  *
  1437.  *----------------------------------------------------------------------
  1438.  */
  1439.  
  1440. void
  1441. regerror(string)
  1442.     char *string;            /* Error message. */
  1443. {
  1444.     tclRegexpError = string;
  1445. }
  1446.